
<template>
    <view class="container">
        <!-- 页面头部 -->
        <view class="page-head">
            <view class="page-head-title">多选框示例</view>
            <view class="page-head-desc">展示不同样式的复选框及交互效果</view>
        </view>

        <!-- 主要内容区 -->
        <view class="content">
            <!-- 基础样式展示 -->
            <view class="section">
                <view class="section-title">基础样式</view>
                <checkbox-group @change="handleBaseChange" class="checkbox-group">
                    <label class="checkbox-item">
                        <checkbox value="option1" checked />已选中选项
                    </label>
                    <label class="checkbox-item">
                        <checkbox value="option2" />未选中选项
                    </label>
                </checkbox-group>
                <view class="selected-info" v-if="baseSelected.length">
                    已选: {{ baseSelected.join(', ') }}
                </view>
            </view>

            <!-- 自定义样式展示 -->
            <view class="section">
                <view class="section-title">自定义样式</view>
                <checkbox-group @change="handleCustomChange" class="checkbox-group">
                    <label class="checkbox-item custom-checkbox">
                        <checkbox value="custom1" checked color="#4cd964" />
                        <text>成功样式</text>
                    </label>
                    <label class="checkbox-item custom-checkbox">
                        <checkbox value="custom2" color="#ff3b30" />
                        <text>警告样式</text>
                    </label>
                    <label class="checkbox-item custom-checkbox">
                        <checkbox value="custom3" color="#007aff" />
                        <text>主要样式</text>
                    </label>
                </checkbox-group>
            </view>

            <!-- 国家选择列表 -->
            <view class="section">
                <view class="section-title">国家选择</view>
                <text class="section-desc">使用uni-list布局的多选列表</text>
                
                <view class="selection-actions">
                    <button 
                        class="action-btn" 
                        size="mini" 
                        @click="selectAll"
                    >
                        {{ allSelected ? '取消全选' : '全选' }}
                    </button>
                    <button 
                        class="action-btn" 
                        size="mini" 
                        @click="clearSelection"
                    >
                        清空选择
                    </button>
                </view>

                <view class="uni-list">
                    <checkbox-group @change="countryChange">
                        <label 
                            class="uni-list-cell uni-list-cell-pd" 
                            v-for="item in countryList" 
                            :key="item.value"
                        >
                            <view class="checkbox-wrapper">
                                <checkbox 
                                    :value="item.value" 
                                    :checked="item.checked" 
                                    @click.stop
                                />
                            </view>
                            <view class="country-name">{{ item.name }}</view>
                            <view class="country-code">{{ item.code }}</view>
                        </label>
                    </checkbox-group>
                </view>

                <view class="selected-countries" v-if="selectedCountries.length">
                    <view class="selected-title">已选择 ({{ selectedCountries.length }})</view>
                    <view class="country-tags">
                        <view class="tag" v-for="country in selectedCountries" :key="country.value">
                            {{ country.name }}
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            // 基础复选框选中值
            baseSelected: ['option1'],
            // 自定义复选框选中值
            customSelected: ['custom1'],
            // 国家列表数据
            countryList: [
                { value: 'USA', name: '美国', code: 'US', checked: false },
                { value: 'CHN', name: '中国', code: 'CN', checked: true },
                { value: 'BRA', name: '巴西', code: 'BR', checked: false },
                { value: 'JPN', name: '日本', code: 'JP', checked: false },
                { value: 'ENG', name: '英国', code: 'GB', checked: false },
                { value: 'FRA', name: '法国', code: 'FR', checked: false }
            ]
        };
    },
    computed: {
        // 计算属性：获取选中的国家
        selectedCountries() {
            return this.countryList.filter(item => item.checked);
        },
        // 计算属性：判断是否全选
        allSelected() {
            return this.countryList.every(item => item.checked);
        }
    },
    methods: {
        // 基础复选框变化处理
        handleBaseChange(e) {
            this.baseSelected = e.detail.value;
        },
        // 自定义复选框变化处理
        handleCustomChange(e) {
            this.customSelected = e.detail.value;
        },
        // 国家选择变化处理
        countryChange(e) {
            const values = e.detail.value;
            this.countryList.forEach(item => {
                this.$set(item, 'checked', values.includes(item.value));
            });
        },
        // 全选/取消全选
        selectAll() {
            const shouldSelectAll = !this.allSelected;
            this.countryList.forEach(item => {
                this.$set(item, 'checked', shouldSelectAll);
            });
        },
        // 清空选择
        clearSelection() {
            this.countryList.forEach(item => {
                this.$set(item, 'checked', false);
            });
        }
    }
};
</script>

<style scoped>
.container {
    background-color: #f5f5f5;
    min-height: 100vh;
}

.page-head {
    padding: 20px 15px;
    background-color: #ffffff;
    text-align: center;
    border-bottom: 1px solid #eee;
}

.page-head-title {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 5px;
}

.page-head-desc {
    font-size: 14px;
    color: #666;
}

.content {
    padding: 15px;
}

.section {
    background-color: #ffffff;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 15px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.section-title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 10px;
    color: #333;
}

.section-desc {
    font-size: 14px;
    color: #666;
    margin-bottom: 15px;
    display: block;
}

.checkbox-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.checkbox-item {
    display: flex;
    align-items: center;
    padding: 8px 0;
    font-size: 14px;
}

.checkbox-item checkbox {
    margin-right: 8px;
}

.custom-checkbox {
    padding: 10px;
    border-radius: 6px;
    transition: background-color 0.2s;
}

.custom-checkbox:hover {
    background-color: #f0f0f0;
}

.custom-checkbox text {
    margin-left: 8px;
}

.selection-actions {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

.action-btn {
    background-color: #007aff;
    color: white;
}

.uni-list {
    background-color: #ffffff;
    border-radius: 6px;
    overflow: hidden;
}

.uni-list-cell {
    display: flex;
    align-items: center;
    padding: 12px 15px;
    border-bottom: 1px solid #f5f5f5;
    transition: background-color 0.2s;
}

.uni-list-cell:last-child {
    border-bottom: none;
}

.uni-list-cell:hover {
    background-color: #f9f9f9;
}

.checkbox-wrapper {
    margin-right: 15px;
}

.country-name {
    flex: 1;
    font-size: 15px;
}

.country-code {
    font-size: 14px;
    color: #999;
    background-color: #f5f5f5;
    padding: 2px 8px;
    border-radius: 12px;
}

.selected-info {
    margin-top: 10px;
    padding: 8px;
    background-color: #f5f5f5;
    border-radius: 4px;
    font-size: 14px;
}

.selected-countries {
    margin-top: 15px;
}

.selected-title {
    font-size: 14px;
    color: #666;
    margin-bottom: 8px;
}

.country-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.tag {
    background-color: #e8f4fd;
    color: #007aff;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 13px;
}
</style>
